<#-- 定义一个换行变量 后续就可以换行了 -->
<#assign lineFeed = "\n\t\t">
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${mapper}.${table.alias}Mapper">

    <resultMap id="BaseResultMap" type="${po}.${table.alias}PO">
        <#list table.columnList as map>
        <#if map.column == "ID">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <#else>
        <result column="${map.column}" jdbcType="${map.jdbcType}" property="${map.property}"/>
        </#if>
        </#list>
    </resultMap>

    <resultMap id="${table.alias}ResultMap" type="${po}.${table.alias}DTO">
        <#list table.columnList as map>
        <#if map.column == "ID">
        <id column="ID" jdbcType="VARCHAR" property="id"/>
        <#else>
        <result column="${map.column}" jdbcType="${map.jdbcType}" property="${map.property}"/>
        </#if>
        </#list>
    </resultMap>

<#if ifNeedStatistics==true>
    <resultMap id="CountResultMap" type="${counts}.CountsDTO">
        <result column="TOTAL_AMOUNT" jdbcType="DECIMAL" property="totalAmount"/>
        <result column="TOTAL_COUNT" jdbcType="INTEGER" property="totalCount"/>
        <result column="SUCCESS_AMOUNT" jdbcType="DECIMAL" property="successAmount"/>
        <result column="SUCCESS_COUNT" jdbcType="INTEGER" property="successCount"/>
    </resultMap>
</#if>

    <sql id="Base_Column_List">
        <#list table.columnList as map>${map.column}<#if map_has_next>,</#if> </#list>
    </sql>

    <select id="queryBySelective" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        <where>
            <#list table.columnList as map>
            <if test="${map.property} != null<#if map.jdbcType=='VARCHAR'> and ${map.property} != ''</#if>">
                AND ${map.column} = <#noparse>#</#noparse>{${map.property},jdbcType=${map.jdbcType}}
            </if>
            </#list>
            <if test="idList != null and idList.size() > 0">
            AND ID IN
            <foreach collection="idList" item="id" open="(" separator="," close=")">
            <#noparse>#</#noparse>{id}
            </foreach>
            </if>
        </where>
    </select>

    <select id="selectByPrimaryKey" resultMap="BaseResultMap">
        select
        <include refid="Base_Column_List"/>
        from ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        where ID = <#noparse>#</#noparse>{id}
    </select>

    <select id="selectByPrimaryKeys"  resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        WHERE ID IN
        <foreach collection="idList" item="id" open="(" separator="," close=")">
            <#noparse>#</#noparse>{id}
        </foreach>
    </select>

    <insert id="insert" parameterType="${po}.${table.alias}PO">
        INSERT INTO ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        (<#list table.columnList as map><#if map_index%3==0 && map_index !=0>${lineFeed}</#if>${map.column}<#if map_has_next>,</#if> </#list>)
        values
        (<#list table.columnList as map><#if map_index%3==0 && map_index !=0>${lineFeed}</#if><#noparse>#</#noparse>{${map.property},jdbcType=${map.jdbcType}}<#if map_has_next>,</#if></#list>)
    </insert>

    <insert id="insertSelective" parameterType="${po}.${table.alias}PO">
        INSERT INTO ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <#list table.columnList as map>
            <if test="${map.property} != null<#if map.jdbcType=='VARCHAR'> and ${map.property} != ''</#if>">
                ${map.column},
            </if>
            </#list>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <#list table.columnList as map>
            <if test="${map.property} != null<#if map.jdbcType=='VARCHAR'> and ${map.property} != ''</#if>">
                <#noparse>#</#noparse>{${map.property},jdbcType=${map.jdbcType}},
            </if>
            </#list>
        </trim>
    </insert>

    <delete id="deleteByPrimaryKey" parameterType="java.lang.String">
        DELETE FROM ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        WHERE ID = <#noparse>#</#noparse>{id,jdbcType=VARCHAR}
    </delete>

    <delete id="deleteByPrimaryKeys">
        DELETE FROM ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        WHERE ID IN
        <foreach collection="idList" item="id" open="(" separator="," close=")">
        <#noparse>#</#noparse>{id}
        </foreach>
    </delete>

    <update id="updateByPrimaryKeySelective" parameterType="${po}.${table.alias}PO">
        UPDATE ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        <set>
        <#list table.columnList as map>
            <#if map.column != "ID">
            <if test="${map.property} != null and ${map.property} != ''">
                ${map.column} = <#noparse>#</#noparse>{${map.property},jdbcType=${map.jdbcType}},
            </if>
            </#if>
        </#list>
        </set>
        WHERE ID = <#noparse>#</#noparse>{id,jdbcType=VARCHAR}
    </update>

    <update id="updateByPrimaryKey" parameterType="${po}.${table.alias}PO">
        UPDATE ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        SET
        (<#list table.columnList as map><#if map_index%3==0 && map_index !=0>${lineFeed}</#if>${map.column}<#if map_has_next>,</#if> </#list>)
        WHERE ID = <#noparse>#</#noparse>{id,jdbcType=VARCHAR}
    </update>

    <select id="listByParam" parameterType="${param}.${table.alias}Param" resultMap="${table.alias}ResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        where 1=1
    </select>

<#if ifNeedStatistics==true>
    <select id="countByParam" parameterType="${param}.${table.alias}Param" resultMap="CountResultMap">
        select
        sum(amount) total_amount,
        count(1) total_count,
        sum(case when pay_state = '2' then amount else null end) success_amount,
        count(case when pay_state = '2' then 1 else null end) success_count
        FROM ${table.alias}<#if ifSpiltByEnt==true>_<#noparse>$</#noparse>{entNum}</#if>
        where 1=1
    </select>
</#if>
</mapper>